//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PdeSecondOrderParabolic.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/TransformedGrid.h>
#include <gen/QL/Methods/Finitedifferences/TridiagonalOperator.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::CPdeSecondOrderParabolic (boost::shared_ptr<QuantLib::PdeSecondOrderParabolic>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPdeSecondOrderParabolic = NULL;
#endif
	_ppPdeSecondOrderParabolic = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::CPdeSecondOrderParabolic (QuantLib::PdeSecondOrderParabolic& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPdeSecondOrderParabolic = NULL;
#endif
	_ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (&childNative);
    
    _PdeSecondOrderParabolicOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::CPdeSecondOrderParabolic (CPdeSecondOrderParabolic^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPdeSecondOrderParabolic = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::CPdeSecondOrderParabolic (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPdeSecondOrderParabolic = NULL;
#endif
	if (!t->IsSubclassOf(CPdeSecondOrderParabolic::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::CPdeSecondOrderParabolic (QuantLib::Handle<QuantLib::PdeSecondOrderParabolic>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPdeSecondOrderParabolic = &childNative;
	_ppPdeSecondOrderParabolic = &static_cast<boost::shared_ptr<QuantLib::PdeSecondOrderParabolic>>(childNative.currentLink());
    
    _PdeSecondOrderParabolicOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::CPdeSecondOrderParabolic (QuantLib::Handle<QuantLib::PdeSecondOrderParabolic> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPdeSecondOrderParabolic = &childNative;
	_ppPdeSecondOrderParabolic = &static_cast<boost::shared_ptr<QuantLib::PdeSecondOrderParabolic>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::CPdeSecondOrderParabolic (QuantLib::PdeSecondOrderParabolic childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPdeSecondOrderParabolic = NULL;
#endif
	_ppPdeSecondOrderParabolic = new boost::shared_ptr<QuantLib::PdeSecondOrderParabolic> (new QuantLib::PdeSecondOrderParabolic (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::~CPdeSecondOrderParabolic ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPdeSecondOrderParabolic != NULL)
    {
	    delete _ppPdeSecondOrderParabolic;
        _ppPdeSecondOrderParabolic = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::!CPdeSecondOrderParabolic ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPdeSecondOrderParabolic != NULL)
    {
	    delete _ppPdeSecondOrderParabolic;
    }
}
QuantLib::PdeSecondOrderParabolic& Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::GetReference ()
{
    if (_ppPdeSecondOrderParabolic == NULL) throw REFNEW NativeNullException ();
	return **_ppPdeSecondOrderParabolic;
}
boost::shared_ptr<QuantLib::PdeSecondOrderParabolic>& Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::GetShared ()
{
    if (_ppPdeSecondOrderParabolic == NULL) throw REFNEW NativeNullException ();
	return *_ppPdeSecondOrderParabolic;
}
QuantLib::PdeSecondOrderParabolic* Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::GetPointer ()
{
    if (_ppPdeSecondOrderParabolic == NULL) throw REFNEW NativeNullException ();
	return &**_ppPdeSecondOrderParabolic;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PdeSecondOrderParabolic>& Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::GetHandle ()
{
	if (_phPdeSecondOrderParabolic == NULL)
	{
		_phPdeSecondOrderParabolic = new Handle<QuantLib::PdeSecondOrderParabolic> (*_ppPdeSecondOrderParabolic);
	}
	return *_phPdeSecondOrderParabolic;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::HasNative () 
{
	return (_ppPdeSecondOrderParabolic != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeSecondOrderParabolic)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::Discount (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeSecondOrderParabolic)->discount ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppPdeSecondOrderParabolic)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::IPdeSecondOrderParabolic^ Cephei::QL::Methods::Finitedifferences::CPdeSecondOrderParabolic::GenerateOperator (Double t, Cephei::QL::Math::ITransformedGrid^ tg, Cephei::QL::Methods::Finitedifferences::ITridiagonalOperator^ L)
{
    CTransformedGrid^ _Ctg;
    CTridiagonalOperator^ _CL;
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        _Ctg = safe_cast<CTransformedGrid^> (tg);
        _Ctg->Lock();
        QuantLib::TransformedGrid& _tg = static_cast<QuantLib::TransformedGrid&> (_Ctg->GetReference ()); 
        _CL = safe_cast<CTridiagonalOperator^> (L);
        _CL->Lock();
        QuantLib::TridiagonalOperator& _L = static_cast<QuantLib::TridiagonalOperator&> (_CL->GetReference ()); 
    	(*_ppPdeSecondOrderParabolic)->generateOperator ( _t,  _tg,  _L );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctg != nullptr) _Ctg->Unlock();
        if (_CL != nullptr) _CL->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

